package cn.lkx.eduservice.service.impl;

import cn.lkx.eduservice.entity.EduChapter;
import cn.lkx.eduservice.entity.EduVideo;
import cn.lkx.eduservice.entity.chapter.ChapterVo;
import cn.lkx.eduservice.entity.chapter.VideoVo;
import cn.lkx.eduservice.mapper.EduChapterMapper;
import cn.lkx.eduservice.service.EduChapterService;
import cn.lkx.eduservice.service.EduVideoService;
import cn.lkx.servicebase.exceptionhandle.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lkx
 * @since 2022-05-03
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    //根据课程id获取所有章节和小节
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //最终要的到的数据列表
        ArrayList<ChapterVo> finalChapterVoList = new ArrayList<>();
        //1、根据课程id查询出所有章节
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id",courseId); //根据id
        chapterWrapper.orderByAsc("gmt_create"); //按日期升序
        List<EduChapter> eduChapterList = baseMapper.selectList(chapterWrapper);
        //2、同样查询出所有小节
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);  //根据id
        videoWrapper.orderByAsc("gmt_create"); //按日期升序
        List<EduVideo> eduVideoList = videoService.list(videoWrapper);
        //3、遍历章节list 进行封装
        for (int i=0; i<eduChapterList.size(); i++){
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo(); //封装进这
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalChapterVoList.add(chapterVo); //放进最终结果集
            //4、遍历小节list 进行封装
            ArrayList<VideoVo> videoVoList = new ArrayList<>();
            for (int j=0; j<eduVideoList.size(); j++){
                EduVideo eduVideo = eduVideoList.get(j);
                //获取该小节对应的章节id,封装进正确的章节
                if(eduChapter.getId().equals( eduVideo.getChapterId() )){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }

        return finalChapterVoList;
    }
    //删除章节方法
    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        int count = videoService.count(queryWrapper);
        if(count > 0){  //有小节，不给删除
            throw new MyException(20001,"该分章节下存在视频课程，请先删除视频课程");
        }else {
            int delete = baseMapper.deleteById(chapterId);
            return delete>0;
        }
    }

    @Override
    public boolean removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        int count = baseMapper.delete(queryWrapper);
        return count>0;
    }
}
